package com.stillalive.mq.mqserver.datacenter;

import com.stillalive.mq.MqApplication;
import com.stillalive.mq.mqserver.core.Binding;
import com.stillalive.mq.mqserver.core.Exchange;
import com.stillalive.mq.mqserver.core.ExchangeType;
import com.stillalive.mq.mqserver.core.MSGQueue;
import com.stillalive.mq.mqserver.mapper.MetaMapper;

import java.io.File;
import java.util.List;

public class DataBaseManager {

    private MetaMapper metaMapper;
    // 通过这个方法对数据库进行初始化操作，并插入一些默认数据
    public void init(){
        metaMapper = MqApplication.context.getBean(MetaMapper.class);
        // 这里有一个问题，如果data/meta.db存在，此时不会执行后续建表等操作
        if(!checkDBExists()){
            // 先创建data目录
            File dataDir = new File("./data");
            dataDir.mkdirs();
            createTable();
            createDefaultData();
            System.out.println("[DataBaseManager] 数据库初始化完成！");
        } else{
            System.out.println("[DataBaseManager] 数据库已经存在！");
        }
    }
    public void deleteDB(){
        File file = new File("./data/meta.db");
        boolean ret = file.delete();
        if (ret) {
            System.out.println("[DataBaseManager] 删除数据库文件成功");
        }else {
            System.out.println("[DataBaseManager] 删除数据库文件失败");
        }

        File dataDir = new File("./data");
        // 使用delete删除目录的时候需要保证目录是空的
        ret = dataDir.delete();
        if (ret) {
            System.out.println("[DataBaseManager] 删除数据库目录成功");
        }else {
            System.out.println("[DataBaseManager] 删除数据库目录失败");
        }
    }
    private boolean checkDBExists() {
        File file = new File("./data/meta.db");
        if(file.exists()){
            return true;
        }
        return false;
    }
    // 这里仅需要建表，建库操作会在代码首次执行的时候自动创建meta.db文件（mybatis实现的）
    private void createTable() {
        metaMapper.createExchangeTable();
        metaMapper.createQueueTable();
        metaMapper.createBindingTable();
        System.out.println("[DataBaseManager] 创建表完成！");
    }

    // 添加一个默认的交换机（匿名的，类型是DIRECT）
    private void createDefaultData() {
        Exchange exchange = new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);
        metaMapper.insertExchange(exchange);
        System.out.println("[DataBaseManager] 创建初始数据完成！");
    }

    public void insertExchange(Exchange exchange){
        metaMapper.insertExchange(exchange);
    }
    public List<Exchange> selectAllExchanges(){
        return metaMapper.selectAllExchanges();
    }
    public void deleteExchange(String exchangeName){
        metaMapper.deleteExchange(exchangeName);
    }
    public void insertQueue(MSGQueue queue){
        metaMapper.insertQueue(queue);
    }
    public List<MSGQueue> selectAllQueues(){
        return metaMapper.selectAllQueues();
    }
    public  void deleteQueue(String queueName){
        metaMapper.deleteQueue(queueName);
    }
    public void insertBinding(Binding binding){
        metaMapper.insertBinding(binding);
    }
    public List<Binding> selectAllBindings(){
        return metaMapper.selectAllBindings();
    }
    public void deleteBinding(Binding binding){
        metaMapper.deleteBinding(binding);
    }
}
